package com.wnzt.baszh.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.Service;
import javax.xml.rpc.encoding.XMLType;

import org.apache.axis.client.Call;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wnzt.baszh.entity.Tsysuser;
import com.wnzt.baszh.service.IMedicalShootService;
import com.wnzt.baszh.service.ISystemMaintenService;
import com.wnzt.baszh.util.common.BaseController;
import com.wnzt.baszh.util.common.ConstantsUtils;
import com.wnzt.baszh.util.common.FileEncodeAndDecode;
import com.wnzt.baszh.util.common.GlobalStatic;
import com.wnzt.baszh.util.common.PageHelper;
import com.wnzt.baszh.util.common.SetSystemProperty;

/**
 * 数字化病案管理
 * @author dqw
 * @date 2017-3-13
 */

@Controller
@RequestMapping("/MedicalShootManage")
public class MedicalShootController extends BaseController{
    
    public static final String KEY = "key"; //加密密匙
    
    public static String DECODEPATH = "";  //解密后的路径
    
    public static final FileEncodeAndDecode encode = new FileEncodeAndDecode(KEY); //加密方法
    
    @Autowired
    private IMedicalShootService medicalShootService;
    
    @Autowired
    private ISystemMaintenService systemMaintenService;
	
	/**
	 * 跳转数字病案拍摄页面
	 * @return
	 */
	@RequestMapping("/RecordShoot")
    public String RecordShoot() {
        return "MedicalShootManage/RecordShoot";
    }
	
	/**
	 * 跳转数字病案审核页面
	 * @return
	 */
	@RequestMapping("/RecordAudit")
	public String RecordAudit() {
		return "MedicalShootManage/RecordAudit";
	}
	
	/**
	 * 跳转数字病案拍摄页面
	 * @return
	 */
	@RequestMapping("/QuestionPreview")
    public String QuestionPreview() {	
        return "MedicalShootManage/QuestionPreview";
    }
	/**
	 * 跳转数字病案浏览页面
	 * @return
	 */
	@RequestMapping("/PicturePreview")
    public String PicturePreview() {	
        return "MedicalShootManage/PicturePreview";
    }
	/**
	 * 跳转图片浏览查询页面
	 * @return
	 */
	@RequestMapping("/PicturePreviewList")
    public String PicturePreviewList() {	
        return "MedicalShootManage/PicturePreviewList";
    }
	
	/**
	 * 跳转数字化病案浏览审核
	 * @return
	 */
	@RequestMapping("/BrowseAudit")
    public String BrowseAudit() {	
        return "Browse/BrowseAudit";
    }
	
	/**
	 * 跳转数字化病案拍摄页面
	 * @return
	 */
	@RequestMapping("/Shoot")
    public String Shoot() {	
        return "MedicalShootManage/Shoot";
    }
	
	/**
	 * 跳转数字化病案拍摄页面
	 * @return
	 */
	@RequestMapping("/BrowsePage")
	public String BrowsePage() {	
		return "Browse/BrowsePage";
	}
	
	/**
	 * 病案集中归档-电子病历归档
	 * @author [lv tang wei]
	 * Describe:
	 * @return
	 * [2017年7月3日]
	 */
	@RequestMapping("/EMRFileBrowse")
	public String EMRFileBrowse(HttpServletRequest request){
		return "Browse/EMRFileBrowsePage";
	}
	//--------------------------------------------------------------------------------------------------------------------------------------------/
	
	/**
	 * 获取出院科室
	 * @param request
	 * @return
	 */
	@RequestMapping("/getDept")
    @ResponseBody
    public Object getDept(HttpServletRequest request){
    	List list = medicalShootService.getDept();
    	return list;
    }
	
	/**
	 * 获取申请目的
	 * @param request
	 * @return
	 */
	@RequestMapping("/getSqmd")
    @ResponseBody
    public Object getSqmd(HttpServletRequest request){
    	List list = medicalShootService.getSqmd();
    	return list;
    }
	
	/**
	 * 获取拍摄页面数据
	 * @param request
	 * @return
	 */
	@RequestMapping("/shootData")
    @ResponseBody
    public Object getShootData(HttpServletRequest request){
		String shootDept = request.getParameter("shootDept");
		String shootDate1 = request.getParameter("shootDate1");
		String shootDate2 = request.getParameter("shootDate2");
		String shootName = request.getParameter("shootName");
		String shootFprn = request.getParameter("shootFprn");
		String shootMode = request.getParameter("shootMode");
		String page = request.getParameter("page");
		String rows = request.getParameter("rows");
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("shootDept", shootDept);
		map.put("shootDate1", shootDate1);
		map.put("shootDate2", shootDate2);
		map.put("shootName", shootName);
		map.put("shootFprn", shootFprn);
		map.put("shootMode", shootMode);
		map.put("page", page);
		map.put("rows", rows);
		PageHelper pageHelper = medicalShootService.getShootData(map);
		return pageHelper;
	}
	
	/**
	 * 获取审核数据
	 * @param request
	 * @return
	 */
	@RequestMapping("/getAuditData")
	@ResponseBody
	public Object getAuditData(HttpServletRequest request){
		Map<String, Object> map =new HashMap<String,Object>();
		String auditDept = request.getParameter("auditDept");
		String auditDate1 = request.getParameter("auditDate1");
		String auditDate2 = request.getParameter("auditDate2");
		String auditName = request.getParameter("auditName");
		String auditFprn = request.getParameter("auditFprn");
		String mode = request.getParameter("mode");
		String page = request.getParameter("page");
		String rows = request.getParameter("rows");
		map.put("auditDept", auditDept);
		map.put("auditDate1", auditDate1);
		map.put("auditDate2", auditDate2);
		map.put("auditName", auditName);
		map.put("auditFprn", auditFprn);
		map.put("mode", mode);
		map.put("page", page);
		map.put("rows", rows);
		PageHelper Page = medicalShootService.getAuditData(map);
		return Page;
	}
	
	/**
	 * 保存图片信息
	 * @param request
	 * @return
	 */
	@RequestMapping("/saveBase64")
	@ResponseBody
	public Object savePicture(HttpServletRequest request){
		//获取当前用户
		Tsysuser user = (Tsysuser) request.getSession().getAttribute(GlobalStatic.USER_SESSION_OBJECT);
		String name = user.getFname();//用户名
		Map<String, Object> map = new HashMap<String,Object>();
		//String base64 = request.getParameter("base64"); //base64编码
	//	String fprn = request.getParameter("fprn");
		String fzyid = request.getParameter("fzyid");
		String pictureName = request.getParameter("pictureName");
		//int ftimes = Integer.parseInt(request.getParameter("ftimes"));
		String address = request.getParameter("address");
		String code = request.getParameter("code");
		String DATE = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
		int pageNumber = Integer.parseInt(request.getParameter("pageNumber")); //图片页面
		//map.put("fprn", fprn);
		map.put("fzyid", fzyid);
		//map.put("ftimes", ftimes);
		map.put("address", address);
		map.put("pageNumber", pageNumber);
		map.put("date", DATE);
		map.put("name", name);
		map.put("code", code);
		//map.put("base64", base64);
		map.put("pictureName", pictureName);
		map.put("size", 0);
		boolean result = medicalShootService.savePicture(map);
		if(result == true){
            //图片加密；前面参数为需要加密的图片，后面参数为加密后的图片文件
            try {
				encode.encrypt(address);
			} catch (Exception e) {
				e.printStackTrace();
			}
            return result;  
		}else{
			return result;
		}
		
	}
	
	/**
	 * 获取图片分类模板
	 * @param request
	 * @return
	 */
	@RequestMapping("/tree")
	@ResponseBody
	public Object tree(HttpServletRequest request) {
		//获取当前用户
		Tsysuser user = (Tsysuser) request.getSession().getAttribute(GlobalStatic.USER_SESSION_OBJECT);
		String name = user.getFname();//用户名
		String fcode = user.getFcode();	//当前用户编号
		String fzyid = request.getParameter("fzyid");
		String fprn = request.getParameter("fprn");
		String DATE = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
		String ip = "";   //获取当前本机IP
		try {
			ip = InetAddress.getLocalHost().toString();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} 
		UUID uuid = UUID.randomUUID(); //获取当前本机uuid
		Map<String, Object> paramMap = new HashMap<String,Object>();
		paramMap.put("fzyid", fzyid);
		paramMap.put("ip", ip);
		paramMap.put("uuid", uuid.toString());
		paramMap.put("fcode", fcode);
		paramMap.put("fprn", fprn);
		paramMap.put("name", name);
		paramMap.put("DATE", DATE);
		
		List<Map<String, Object>> thirdList = medicalShootService.getTree(paramMap); // 根据病案的住院流水号查询病案信息图片
		List<Map<String, Object>> listOptions = medicalShootService.getListOptions(); // 查询病案信息展示列表项
		List<Map<String, Object>> results = new ArrayList<Map<String,Object>>();   
		List<Map<String, Object>> secondList = new ArrayList<Map<String,Object>>();
		//第一层菜单名
		Map<String, Object> map = new HashMap<String,Object>();
		map.put("id", "01");
		map.put("text", "病案封面");
		map.put("children", secondList);
		results.add(map);
		for (Map<String, Object> m : listOptions) {
		    Map<String, Object> map2 = new HashMap<String,Object>();
		    map2.put("id", String.valueOf(m.get("FID")));
            map2.put("text", m.get("FNAME"));
            map2.put("attributes", m.get("FCODE"));
            
            if (!thirdList.isEmpty()) {
                List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
                map2.put("children", list);
                
                for (Map<String, Object> m2 : thirdList) {
                    
                    if(String.valueOf(m2.get("FTPFL")).equals(m.get("FCODE"))) {
                        
                        String filetype = (String) m2.get("ftype");   // 文件类型  1: pdf 0: 图片
                        if ("1".equals(filetype)) {
                            map2.put("fileType", 1);
                            int pageNumber = (Integer) m2.get("ftpym");  // 获取pdf总页数
                            for (int i = 0; i < pageNumber; i++) {
                                Map<String,Object> map3 = new HashMap<String,Object>();
                                map3.put("id", (Integer)m2.get("FID") + i);
                                map3.put("text", i + 1);
                                map3.put("fileName", m2.get("FTPNAME"));
                                map3.put("type", 1);
                                list.add(map3);
                            }
                        } else if(m2.get("ftpym") != null) {
                            Map<String,Object> map3 = new HashMap<String,Object>();
                            map3.put("id", m2.get("FID") + "," + m2.get("FTPBH"));
                            map3.put("text", "第" + m2.get("ftpym") + "页");
                            map3.put("iconCls", "");
                            map3.put("attributes", m2.get("fscdz"));
                            map3.put("type", m2.get("ftype"));
                            list.add(map3);	
                        }
                    }
                }
            }
            secondList.add(map2);
		}
		return results;
	}
	
	/**
	 * 图片审核成功。需将临时文件中的所有图片移动到存储图片的服务器上
	 * @param request
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/auditOperation")	
	@ResponseBody
	public Object auditOperation(HttpServletRequest request ) throws Exception{
		//获取当前用户
		Tsysuser user = (Tsysuser) request.getSession().getAttribute(GlobalStatic.USER_SESSION_OBJECT);
		String name = user.getFname();//用户名
		String fcode = user.getFtyyhbh();
		String path = request.getParameter("path");
		String[] pathArray = path.split(",");
		//String number = request.getParameter("number");
	//	String id = request.getParameter("id");
		String categoryNumber = request.getParameter("categoryNumber");
		//String fprn = request.getParameter("fprn");
		//int ftimes = Integer.parseInt(request.getParameter("ftimes"));
		String barcode = request.getParameter("barcode");
		String fname = request.getParameter("fname");
		String fzyid = request.getParameter("fzyid");
		String DATE = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
		Map<String, Object> map = new HashMap<String ,Object>();
		//保存图片的服务器路径
		//String save = request.getSession().getServletContext().getRealPath("/image/"+fcode+"");   
		//测试用的保存路径
		String save ="D:"+"\\"+"DocImage"+"\\";
		File savePath = new File(save);
		if(!savePath.isDirectory()){
			savePath.mkdir();
		}
		String originPath = "";
		//审核通过后将图片复制到保存图片的服务器
		for (int i = 0; i < pathArray.length; i++) {
			String str = pathArray[i].replaceAll("\\\\", "/");
			 //获取图片原路径
			if(i == 0){  
				originPath = pathArray[0].substring(0,(str.lastIndexOf("/") + 1));
			}
			String suffix = str.substring(str.lastIndexOf("/"),pathArray[i].length());
			File originFile = new File(pathArray[i]);
			if(originFile.exists()){
				FileInputStream in = new FileInputStream(pathArray[i]);
				FileOutputStream out = new FileOutputStream(savePath + suffix);
				byte[] buffer = new byte[1024];
				int ins ;
				while((ins = in.read()) > 0){
					out.write(buffer, 0, ins); 
				}
				out.flush();
				in.close();
				out.close();
			}
			originFile.delete(); //删除临时文件
		}
		map.put("name", name);
		map.put("savePath", save);
		map.put("originPath", originPath);
		//map.put("number", number);
		map.put("categoryNumber", categoryNumber);
	//	map.put("fprn", fprn);
		//map.put("ftimes", ftimes);
		map.put("barcode", barcode);
		map.put("fname", fname);
		map.put("fzyid", fzyid);
		map.put("fcode", fcode);
		//map.put("id", id);
		map.put("date", DATE);
		Map<String, Object> flag = medicalShootService.auditOperation(map);
		return flag;
	}
	
	/**
	 * 图片审核失败。审核失败后需将保存在服务器上的图片全部移动到临时文件中
	 * @param request
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/auditFail")	
	@ResponseBody
	public Object auditFail(HttpServletRequest request ) throws Exception{
		//获取当前用户
		Tsysuser user = (Tsysuser) request.getSession().getAttribute(GlobalStatic.USER_SESSION_OBJECT);
		String name = user.getFname();//用户名
		String fcode = user.getFtyyhbh();
		String path = request.getParameter("path");
		String[] pathArray = path.split(",");
		//String number = request.getParameter("number");
	//	String id = request.getParameter("id");
		String categoryNumber = request.getParameter("categoryNumber");
		//String fprn = request.getParameter("fprn");
		//int ftimes = Integer.parseInt(request.getParameter("ftimes"));
		String barcode = request.getParameter("barcode");
		String fname = request.getParameter("fname");
		String fzyid = request.getParameter("fzyid");
		String DATE = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
		Map<String, Object> map = new HashMap<String ,Object>();
		//审核失败后将已保存的图片全部移动到临时文件
		//String save = request.getSession().getServletContext().getRealPath("/image/"+fcode+"");   
		//测试用的保存路径
		String save ="D:"+"\\"+"Download"+"\\";
		File savePath = new File(save);
		if(!savePath.isDirectory()){
			savePath.mkdir();
		}
		String originPath = "";
		//审核通过后将图片复制到保存图片的服务器
		for (int i = 0; i < pathArray.length; i++) {
			String str = pathArray[i].replaceAll("\\\\", "/");
			 //获取图片原路径
			if(i == 0){  
				originPath = pathArray[0].substring(0,(str.lastIndexOf("/") + 1));
			}
			String suffix = str.substring(str.lastIndexOf("/"),pathArray[i].length());
			File originFile = new File(pathArray[i]);
			if(originFile.exists()){
				FileInputStream in = new FileInputStream(pathArray[i]);
				FileOutputStream out = new FileOutputStream(savePath + suffix);
				byte[] buffer = new byte[1024];
				int ins ;
				while((ins = in.read()) > 0){
					out.write(buffer, 0, ins); 
				}
				out.flush();
				in.close();
				out.close();
			}
			originFile.delete(); //删除已保存的文件
		}
		map.put("name", name);
		map.put("savePath", save);
		map.put("originPath", originPath);
		//map.put("number", number);
		map.put("categoryNumber", categoryNumber);
	//	map.put("fprn", fprn);
		//map.put("ftimes", ftimes);
		map.put("barcode", barcode);
		map.put("fname", fname);
		map.put("fzyid", fzyid);
		map.put("fcode", fcode);
		//map.put("id", id);
		map.put("date", DATE);
		Map<String, Object> flag = medicalShootService.auditFail(map);
		return flag;
	}
	
	/**
	 * 图片错误上报
	 * @param request
	 * @return
	 */
	@RequestMapping("/errorReport")
	@ResponseBody
	public Object errorReport(HttpServletRequest request){
		//获取当前用户
		Tsysuser user = (Tsysuser) request.getSession().getAttribute(GlobalStatic.USER_SESSION_OBJECT);
		String name = user.getFname();//用户名
		String fname = request.getParameter("fname");
	//	int ftimes = Integer.parseInt(request.getParameter("ftimes"));
	//	String fprn = request.getParameter("fprn");
		String fzyid = request.getParameter("fzyid");
		String imgNumber = request.getParameter("imgNumber");
		String category = request.getParameter("category");
		String text = request.getParameter("text");
		String barcode = request.getParameter("barcode");
		String reason = request.getParameter("reason");
		int pageCode = Integer.parseInt(request.getParameter("pageCode"));
		String DATE = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
		Map<String, Object> map = new HashMap<String,Object>();
		map.put("fname", fname);
//		map.put("ftimes",ftimes );
//		map.put("fprn", fprn);
		map.put("imgNumber", imgNumber);
		map.put("category", category);
		map.put("text", text);
		map.put("barcode", barcode);
		map.put("date", DATE);
		map.put("name", name);
		map.put("pageCode", pageCode);
		map.put("reason", reason);
		map.put("fzyid", fzyid);
		Map<String, Object> result = medicalShootService.errorReport(map);
		return result;
		
	}
	
	/**
	 * 查询审核记录
	 * @param request
	 * @return
	 */
	@RequestMapping("/reason")
	@ResponseBody
	public Object getReasonRecord(HttpServletRequest request){
		String fprn = request.getParameter("fprn");
		String fzyid = request.getParameter("fzyid");
		String number = request.getParameter("number");
		int ftimes = Integer.parseInt(request.getParameter("ftimes"));
		Map<String, Object> map = new HashMap<String ,Object>();
		map.put("fprn", fprn);
		map.put("number", number);
		map.put("fzyid", fzyid);
		map.put("ftimes", ftimes);
		List<Map<String, Object>> list = medicalShootService.getReasonRecord(map);
		return list;
	}
	
	/**
	 * 插入图片
	 * @param request
	 * @return
	 */
	@RequestMapping("/appendImg")
	@ResponseBody
	public Object appendImg(HttpServletRequest request){
		//获取当前用户
		Tsysuser user = (Tsysuser) request.getSession().getAttribute(GlobalStatic.USER_SESSION_OBJECT);
		String name = user.getFname();//用户名
	//	String fprn = request.getParameter("fprn");
		String fzyid = request.getParameter("fzyid");
	//	int ftimes = Integer.parseInt(request.getParameter("ftimes"));
		int pageNumber = Integer.parseInt(request.getParameter("pageNumber"));
		String category = request.getParameter("category");
		String originPath = request.getParameter("originPath");
		String fbarcode = request.getParameter("fbarcode");
		String savePath = request.getParameter("savePath");
		String imgName = request.getParameter("imgName");
		//String base64 = request.getParameter("base64");
		String DATE = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
		boolean isChildren = Boolean.parseBoolean(request.getParameter("isChildren"));
		Map<String, Object> map = new HashMap<String,Object>();
		map.put("name", name);
		//map.put("fprn", fprn);
		map.put("fzyid", fzyid);
		//map.put("ftimes", ftimes);
		map.put("pageNumber", pageNumber);
		map.put("category", category);
		map.put("originPath", originPath);
		map.put("fbarcode", fbarcode);
		map.put("savePath", savePath);
		map.put("imgName", imgName);
	//	map.put("base64", base64);
		map.put("date", DATE);
		map.put("isChildren", isChildren);
		map.put("size", 0);
		boolean flag = medicalShootService.appendImg(map);
		if(flag == true){
            //图片加密；前面参数为需要加密的图片，后面参数为加密后的图片文件
            try {
				encode.encrypt(savePath);
			} catch (Exception e) {
				e.printStackTrace();
			}
            return flag;  
		}else{
			return flag;
		}
	}
	
	/**
	 * 重拍
	 * @param request
	 * @return
	 */
	@RequestMapping("/repeatShoot")
	@ResponseBody
	public Object repeatShoot(HttpServletRequest request){
		//获取当前用户
		Tsysuser user = (Tsysuser) request.getSession().getAttribute(GlobalStatic.USER_SESSION_OBJECT);
		String name = user.getFname();//用户名
		//String fprn = request.getParameter("fprn");
		String fzyid = request.getParameter("fzyid");
		//int ftimes = Integer.parseInt(request.getParameter("ftimes"));
		//String base64Code = request.getParameter("base64Code");
		String fbarcode = request.getParameter("fbarcode");
		String originPath = request.getParameter("originPath");
		String fatherCategory = request.getParameter("fatherCategory");
		int pageNumber = Integer.parseInt(request.getParameter("pageNumber"));
		String saveFileName = request.getParameter("saveFileName");
		String imgName = request.getParameter("imgName");
		String id = request.getParameter("id");
		String DATE = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
		Map<String, Object> map = new HashMap<String,Object>();
		map.put("name",name);
		//map.put("fprn", fprn);
		//map.put("ftimes", ftimes);
		map.put("fzyid", fzyid);
		//map.put("base64Code", base64Code);
		map.put("size", 0);
		map.put("fbarcode", fbarcode);
		map.put("originPath", originPath);
		map.put("fatherCategory", fatherCategory);
		map.put("pageNumber", pageNumber);
		map.put("saveFileName", saveFileName);
		map.put("imgName", imgName);
		map.put("id", id);
		map.put("date", DATE);
		boolean result = medicalShootService.repeatShoot(map);
		if(result ==true){
			//删除替换的图片
			File file = new File(saveFileName);
			if(file.exists()){
				file.delete();
			}
            return result;  
		}else{
			return false;
			
		}
	}
	
	/**
	 * 加密图片
	 * @param request
	 * @return
	 */
	@RequestMapping("/createPicture")
	@ResponseBody
	public Object createPicture(HttpServletRequest request){
		String imgName = request.getParameter("strFileName");
		Map<String, Object> map = new HashMap<String,Object>();
		if(ConstantsUtils.isNotBank(imgName)){
			 //图片加密；前面参数为需要加密的图片，后面参数为加密后的图片文件
            try {
				encode.encrypt(imgName);
				map.put("msg", true);
			} catch (Exception e) {
				e.printStackTrace();
			}
            
		}else{
			map.put("msg", false);
		}
		return map;
	}
	
	/**
	 * 删除照片
	 * @param request
	 * @return
	 */
	@RequestMapping("/removePicture")
	@ResponseBody
	public Object removePicture(HttpServletRequest request){
	//	String fprn = request.getParameter("fprn");
		String fzyid = request.getParameter("fzyid");
		//int ftimes = Integer.parseInt(request.getParameter("ftimes"));
		String fatherCategory = request.getParameter("fatherCategory");
		int pageNumber = Integer.parseInt(request.getParameter("pageNumber"));
		String id = request.getParameter("id");
		String path = request.getParameter("path");
		Map<String, Object> map = new HashMap<String,Object>();
	//	map.put("fprn", fprn);
		//map.put("ftimes", ftimes);
		map.put("fatherCategory", fatherCategory);
		map.put("pageNumber", pageNumber);
		map.put("id", id);
		map.put("path", path);
		map.put("fzyid", fzyid);
		boolean result = medicalShootService.removePicture(map);
		if(result == true){
			File file = new File(path);
			if(file.exists()){
				file.delete();
			}
		}else{
			return false;
		}
		return result;
	}
	
	/**
	 * 获取错误上报的数据
	 * @param request
	 * @return
	 */
	@RequestMapping("/questionData")
	@ResponseBody
	public Object questionData(HttpServletRequest request){
		String fprn = request.getParameter("fprn");
		String fzyid = request.getParameter("fzyid");
		int ftimes = Integer.valueOf(request.getParameter("ftimes"));
		String page = request.getParameter("page");
		String rows = request.getParameter("rows");
		Map<String, Object> map = new HashMap<String,Object>();
		map.put("fprn", fprn);
		map.put("ftimes", ftimes);
		map.put("fzyid", fzyid);
		map.put("page", page);
		map.put("rows", rows);
		PageHelper pageHelper = medicalShootService.getQuestionData(map);
		return pageHelper;
	}
	
	/**
	 * 获取主治医生
	 * @param request
	 * @return
	 */
	@RequestMapping("/getZzDoc")
	@ResponseBody
	public Object getZzDoc(HttpServletRequest request){
		List<Map<String, Object>> list = medicalShootService.getZzDoc();
		return list;
	}
	
	/**
	 * 获取病案分类
	 * @param request
	 * @return
	 */
	@RequestMapping("/getCategory")
	@ResponseBody
	public Object getCategory(HttpServletRequest request){
		List<Map<String, Object>> list = medicalShootService.getCategory();
		return list;
	}
	
	
	/**
	 * 获取疾病
	 * @param request
	 * @return
	 */
	@RequestMapping("/getJB")
	@ResponseBody
	public Object getJB(HttpServletRequest request){
		List<Map<String, Object>> list = medicalShootService.getJB();
		return list;
	}
	
	
	/**
	 * 获取浏览页数据
	 * @param request
	 * @return
	 */
	@RequestMapping("/previewListSearch")
	@ResponseBody
	public Object PreviewListSearch(HttpServletRequest request){
		String date1 = request.getParameter("date1");
		String date2 = request.getParameter("date2");
		String fcydept = request.getParameter("fcydept");
		String fzzDoc = request.getParameter("fzzDoc");
		String fprn = request.getParameter("fprn");
		String ftimes = request.getParameter("ftimes");
		String fbarcode = request.getParameter("fbarcode");
		String fname = request.getParameter("fname");
		String fsex = request.getParameter("fsex");
		String category = request.getParameter("category");
		String page = request.getParameter("page");
		String rows = request.getParameter("rows");
		String fjb = request.getParameter("fjb");
		Map<String, Object> map = new HashMap<String,Object>();
		map.put("date1", date1);
		map.put("date2", date2);
		map.put("fcydept", fcydept);
		map.put("fzzDoc", fzzDoc);
		map.put("fprn", fprn);
		map.put("ftimes", ftimes);
		map.put("fbarcode", fbarcode);
		map.put("fname", fname);
		map.put("fsex", fsex);
		map.put("category", category);
		map.put("fjb", fjb);
		map.put("page", page);
		map.put("rows", rows);
		PageHelper pageHelper = medicalShootService.PreviewListSearch(map);
		return pageHelper;
	}
	
	/**
	 * 解密图片
	 * @param request
	 * @return
	 */
	@RequestMapping("/encodeIMG")
	@ResponseBody
	public Object encodeIMG(HttpServletRequest request){
		//获取当前用户
		Tsysuser user = (Tsysuser) request.getSession().getAttribute(GlobalStatic.USER_SESSION_OBJECT);
		String fcode = user.getFcode();//用户名
		String originPath = request.getParameter("path");
		boolean isMore = Boolean.parseBoolean(request.getParameter("isMore"));
		//获取项目根路径
		String curPath = request.getSession().getServletContext().getRealPath("/upload/"+fcode+"");
		String str = "";
		//获取图片名
		String fileName = "";
		String postfix = ".jpg";
		String path = "";
		Map<String, Object> map = new HashMap<String , Object>();
		//如果为true，则表示要解密多个图片
		if(isMore){ 
			String[] pathArray = originPath.split(",");
			for (int i = 0; i < pathArray.length; i++) {
				 str = pathArray[i].replaceAll("\\\\", "/");
				 fileName = str.substring(str.lastIndexOf("/")+1,str.lastIndexOf("."));
				 path = curPath +"\\"+ fileName + postfix;
				 try {
					 encode.decrypt(pathArray[i], path); // 对图片解密
					 medicalShootService.watermark(user, new File(path), fileName + postfix, curPath);  // 给图片添加水印
					 map.put("msg", true);
				 } catch (Exception e) {
					 map.put("msg", false);
					 e.printStackTrace();
				 }
			}
		}else{
			 str = originPath.replaceAll("\\\\", "/");
			 fileName = str.substring(str.lastIndexOf("/")+1,str.lastIndexOf("."));
			 path = curPath +"\\"+ fileName + postfix;
			 try {
				 encode.decrypt(originPath, path);  // 对图片解密
				 medicalShootService.watermark(user, new File(path), fileName + postfix, curPath); // 给图片添加水印
				 map.put("msg", true);
			 } catch (Exception e) {
				 map.put("msg", false);
				 e.printStackTrace();
			 }
		}
		return map;
		
	}
	
	/**
	 * 删除临时文件夹 
	 * @param request
	 * @return
	 */
	@RequestMapping("/deleteTemporaryFolder")
	@ResponseBody
	public Object deleteTemporaryFolder(HttpServletRequest request){
		String fcode = request.getParameter("fcode");
		String path = request.getSession().getServletContext().getRealPath("/upload/"+fcode+"");
		File file = new File(path);
		boolean result = deleteFile(file);
		return result;
	}
	
	
	/**
	 * 用递归批量删除文件
	 */
	public boolean deleteFile(File file){
		if(file.exists()){
			//判断是否文件
			if(file.isFile()){
				file.delete();
			}else if(file.isDirectory()){
				//声明目录下所有的文件 files[];  
				File[] files = file.listFiles();
				//遍历目录下所有文件，用递归删除所有目录下的文件
				for (int i = 0; i < files.length; i++) {
					this.deleteFile(files[i]);
				}
				//删除所有文件后删除文件夹
				file.delete();
			}
			return true;
		}else{
			System.out.println("文件夹不存在！");
			return false;
		}
	}
	
	/**
	 * 获取页面按钮权限
	 */
	@RequestMapping("getBtnPermision")
	@ResponseBody
	public Object getBtnPermision(HttpServletRequest request){
		
		Tsysuser user = (Tsysuser) request.getSession().getAttribute(GlobalStatic.USER_SESSION_OBJECT);//获取当前用户
		String tabtext = request.getParameter("tabtext");//获取当前页面标题
		return medicalShootService.getBtnPermision(user,tabtext);
	}

    /**
     * 传送拍摄信息
     * @param request
     * @return
     */
    @RequestMapping("transfer")
    @ResponseBody
    public Object transfer(HttpServletRequest request){
        Tsysuser user = (Tsysuser) request.getSession().getAttribute(GlobalStatic.USER_SESSION_OBJECT);//获取当前用户
        String name = user.getFname(); 
        String fcode = user.getFtyyhbh(); //统一编号
        String fzyid = request.getParameter("fzyid");
        String fprn = request.getParameter("fprn");
        int ftimes = Integer.parseInt(request.getParameter("ftimes"));
        String fname = request.getParameter("fname");
        String fcydate = request.getParameter("fcydate");
        String DATE = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("name", name);
        map.put("fcode", fcode);
        map.put("fzyid", fzyid);
        map.put("fprn", fprn);
        map.put("ftimes", ftimes);
        map.put("fname", fname);
        map.put("fcydate", fcydate);
        map.put("DATE", DATE);
        Map<String, Object > resultMap =  medicalShootService.transfer(map);
        return resultMap;
    }
    
	/**
	 * 获取病案上传文件类型列表项
	 * @return
	 */
	@RequestMapping("getFileTypeList")
    @ResponseBody
	public Object getFileTypeList() {
	    return medicalShootService.getListOptions();
	}
	
	/**
	 * 病案集中归档浏览树
	 * @author [lv tang wei]
	 * Describe:  
	 * 1.获取pdf分类
	 * 2.取分类下病案
	 * 3.获取集中归档
	 * 4.读取集中归档信息，每份pdf
	 * @param request
	 * @return
	 * [2017年7月3日]
	 */
	@RequestMapping("/EMRTypeTree")
	@ResponseBody
	public Object EMRTypeTree(HttpServletRequest request) {
		/*请求参数*/
		Map<String, Object> paramMap = this.getParameterMap(request);
		/*所有病历类型*/
		List<Map<String, Object>> bllxList = systemMaintenService.getAllBllx(); 
		long a=System.currentTimeMillis();
		/*病案集中归档 -- 调取电子病历webservice接口*/
		try {
			String rcp_path = Thread.currentThread().getContextClassLoader().getResource("").getPath();
			FileInputStream file = null;
			Properties properties = new Properties();
			file = new FileInputStream(new File(rcp_path+ "config.properties"));
			properties.load(file);
			Service service = new org.apache.axis.client.Service();
			Call call = (Call) service.createCall();
			call.setTargetEndpointAddress(properties.getProperty("archive.webUrl"));/*接口地址*/
			call.setOperationName(new QName(properties.getProperty("archive.OperationName")/*命名空间*/, properties.getProperty("archive.execute")/*执行方法*/));// WSDL里面描述的接口名称
			call.addParameter(properties.getProperty("archive.Parameter")/*参数名*/, XMLType.XSD_STRING, ParameterMode.IN);//
			call.setReturnType(XMLType.XSD_STRING);// 设置返回类型
			call.setProperty(Call.CONNECTION_TIMEOUT_PROPERTY, new Integer(5000));//连接超时设置
			call.setTimeout(new Integer(50000));//超时设置
			//入参参数
			String inParamXml="<?xml version=\"1.0\" encoding=\"UTF-8\"?><data><fzyid>"+paramMap.get("fzyid")+"</fzyid><fprn>"+paramMap.get("fprn")+"</fprn><ftimes>"+paramMap.get("ftimes")+"</ftimes><fname>"+paramMap.get("fname")+"</fname></data>";
			String outParamXml = (String) call.invoke(new Object[] { inParamXml }); //调用接口并取返回值
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("执行耗时 : "+(System.currentTimeMillis()-a)/1000f+" 秒 ");
		List<Map<String,Object>> gdmxList=this.medicalShootService.getBagdmx(paramMap);
		for (Map<String, Object> bllx : bllxList) {
			List<Map<String,Object>> obj=new ArrayList<Map<String,Object>>();
			for (Map<String, Object> mx : gdmxList) {
				if(bllx.get("FCODE").equals(mx.get("FBLLX"))){
					obj.add(mx);
				}
			}
			bllx.put("children", obj);
		}
		return bllxList;
	}
}
